/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETSI_Root.h
  \date			september 2011
  \author		TNick

  \brief		Contains the definition for ETSI_Root class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __ETSI_Root_INC__
#define __ETSI_Root_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<sheet/ETSubItem.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/// sub-item able to host a row of horizontal sub-items
/**
*
*	\section dimensions Dimensions. Bounding box.
*	The bounding box consists of the united bounding boxes for it's sub-items.
*	The sub-item itself does not have any elements and adds nothing to
*	the final box.
*/
class ETSI_Root : public ETSubItem DDHC_INHERIT		{ DDHC_CLASS

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */

	friend class ETI_CplxTxt;

	friend class ETSI_Fract;
	friend class ETSI_HText;
	friend class ETSI_Integ;
	friend class ETSI_Lim;
	friend class ETSI_LogicAc;
	friend class ETSI_Matrix;
	friend class ETSI_Radic;
	friend class ETSI_Sum;
	friend class ETSI_Rows;

	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

private:


	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Logic relations    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// list of top level items; the elements are ETSubItem
	CC_HDL2D    it_lst;


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */



	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

public:

	/// constructor;
	ETSI_Root		( void );

	/// destructor;
	virtual				~ETSI_Root		( void );


	/// informed when a direct kid is about to be removed
	/**
	*	The sub-item is only required to remove the kid from internal
	*	chain if it has one. It is intended to be used by complex
	*	sub-items that may host other sub-items.
	*/
	void				ackRemoveKid	(ETSubItem * kid);




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Dimensions. Bounding box.    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// get the bounding box of this item
	/**
 *	The function may or may not honor the flags representing the "dirty"
 *	state of that item regarding it's characteristics depending on
 *	\b b_full argument (when true the flags are ignored)
 */
	void				recompute		(bool b_full);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Logic relations    */
	/* The list of top level elements is made easy. This group of functions
 * allows users of this class to manipulate the chain of elements
 * using a toponimy similar to that used in base class for the list.
 */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */



protected:

	/// append an item (insert after last)
	inline void			append	(ETSubItem* pIt)
	{ it_lst.append((CC_L2D*)pIt); pIt->setFather(this); }


	/// get the number of items
	inline int			count	( void ) const
	{ return it_lst.count(); }


	/// get nth item
	inline ETSubItem*	item	(int i) const
	{ return (ETSubItem*)it_lst.item(i); }


	/// get first item
	inline ETSubItem*	first	( void )  const
	{ return (ETSubItem*)it_lst.first(); }


	/// get last item
	inline ETSubItem*	last	( void )  const
	{ return (ETSubItem*)it_lst.last(); }

private:


	/// remove all items from this list by deleting them
	inline void			erase		( void )
	{ it_lst.erase(); }



	/// returns the index of a particular item inside this chain
	inline int          itemIndex	( const ETSubItem* pIt)  const
	{ return it_lst.itemIndex((CC_L2D*)pIt); }


	/// tell if an item is part of this chain
	inline bool         has		( const ETSubItem* pIt)  const
	{ return it_lst.has((CC_L2D*)pIt); }



	/// prepend an item (insert as first item)
	inline void			prepend	(ETSubItem* pIt)
	{ it_lst.prepend((CC_L2D*)pIt); }


	/// remove the item at given index
	inline void			remove	(int i)
	{ it_lst.remove(i); }

	/// remove a particular item
	inline void			remove	(ETSubItem* pIt)
	{ it_lst.remove((CC_L2D*)pIt); }

	/*
   /// header is informed about an item being added
   inline void			ackAdd	(ETSubItem* pNew)
   { it_lst.ackAdd((CC_L2D*)pNew); }


   /// header is informed about an item being deleted
   inline void			ackDel	(ETSubItem* pNew)
   { it_lst.ackDel((CC_L2D*)pNew); }

   /// set first item
   inline void			setFirst(ETSubItem* pNew)
   { it_lst.setFirst((CC_L2D*)pNew); }

   /// set number of items
   inline void			setCount(int iNew)
   { it_lst.setCount(iNew); }
   */


	/// insert a item at specified index
	void				insert(ETSubItem* p_new, int index)
	{ it_lst.insert((CC_L2D*)p_new, index); }


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


protected:


	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Others    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// paint the element on provided surface
	void		paint			(
		ETSheetView *	vp,
		QPainter *		painter,
		QPointF			pt_ref
		);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Navigation    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


//	/// recieves navigation keys
//	void		navKey				(
//		ETSheetView *		vp,
//		Qt::Key 			key
//		);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Others    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


protected:

	/// called to determine the sub-item that was hit
	void		hitTest		(HtData * h_data);


	/// aid function to be used by sub-items that embed this one
	bool		hitEmbAid	(HtData * h_data, qreal dx, qreal dy);


public:

	/// identifies the type of the sub-item
	SIType		type		( void ) const { return TYSTEXT;}

	/// dump this object
	void		dumpDbg2		( void ) const
	{ return it_lst.dbgDump(); }


	/// returns the position of the reference point given child in this item's coordinates
	/**
	*	This function is intended for complex items; the argument needs to be a
	*	direct kid. For simple items the default implementation will do.
	*
	*	The function is not required to check if the provided item is trully a kid,
	*	as it is assumed the caller has already done that.
	*/
	virtual QPointF		subIRefPt	(const ETSubItem * p_kid) const;


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Save and load	*/
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

public:

	/// creates a new item based on DOM element's content
	/**
	*	It is the responsability of the caller to properly insert resulted
	*	sub-item in a chain.
	*
	*	@return either the item that was initialised or NULL for errors
	*/
	static ETSI_Root *		loadXML			(
		QDomElement		dom_e,	/**< data source */
		ETSI_Root *		existing = NULL	/**< the caller may pass the address
of the item to change; in this case the return value, in case of success,
is the same as \b existing. */
		);


	/// saves the properties of this item to provided DOM element
	/**
	*	Internally, the inherited class needs to call saveXMLCmn()
	*	to save common properties hosted by the ETSubItem class
	*/
	void					saveXML			(
		QDomElement		dom_e
		);


	/// will load the data concearning an embedded root sub-item
	/**
	*	It will search the provided \b dom_e element for one and
	*	only \b node_name. If found, it will use loadEbdXmlRootSi to
	*	read it's content.
	*/
	static bool				loadEbdXmlRoot		(
			QString 	node_name,
			ETSI_Root *	out_rt,
			QDomElement	dom_e
			);


	/// will load the data concearning an embedded root sub-item
	/**
	*	Searches for a \b subitem element in provided \b dom_e element
	*	and reads it's content in the \b out_rt
	*/
	static bool				loadEbdXmlRootSi		(
		ETSI_Root *		out_rt,
		QDomElement		dom_e
		);

	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */



	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class ETSI_Root	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __ETSI_Root_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
